<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="20">
          <el-form-item>
            <el-input v-model="searchForm.fuzzy" style="width:440px" placeholder="原报告编号/任务单号/项目名称/委托单位/受检单位" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="iconfont icon-chaxun1" @click="handleSearch"> 查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      border
      :data="recoveryData"
      stripe
      highlight-current-row
      :header-cell-style="{background:'#ECF3FC'}"
    >
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="reportCode" label="原报告编号" width="140px" align="center" />
      <el-table-column prop="jobId" label="任务单号" width="140px" align="center" />
      <el-table-column prop="projectName" label="项目名称" width="170px" align="center" />
      <el-table-column prop="consignorName" label="委托单位" width="170px" align="center" />
      <el-table-column prop="inspectionName" label="受检单位" width="170px" align="center" />
      <el-table-column prop="xdbgbh" label="修订报告编号" width="170px" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.reportRecoverDetailVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" v-if="element.reportCodeAmend === null" style="color: #606266;cursor: default;">&nbsp;</el-button>
              <el-button type="text" v-if="element.reportCodeAmend != null" style="color: #606266;cursor: default;">{{ element.reportCodeAmend }}</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="bglx" label="报告类型" width="170px" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.reportRecoverDetailVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <span v-if="element.recoverState === '0'"><el-button type="text" style="color: #fff;cursor: default;background-color: rgb(19, 206, 102);padding-left: 10px;padding-right: 10px;">修订报告</el-button></span>
              <span v-if="element.recoverState === '1'"><el-button type="text" style="color: #fff;cursor: default;background-color: #e64242;padding-left: 10px;padding-right: 10px;">作废报告</el-button></span>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="xdsj" label="修订时间" width="170px" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.reportRecoverDetailVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" v-if="element.amendTime === null" style="color: #606266;cursor: default;">&nbsp;</el-button>
              <el-button type="text" v-if="element.amendTime != null" style="color: #606266;cursor: default;">{{ element.amendTime }}</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="300px">
        <div slot-scope="{row}">
          <div v-for="element in row.reportRecoverDetailVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" v-if="element.recoverState === '0'" @click="seeRecover(element)">查看修订原因</el-button>
              <el-button type="text" v-if="element.recoverState === '1'" @click="seeRepeal(element)">查看作废原因</el-button>
              <el-button type="text" v-if="element.recoverState === '0'" @click="revisionDetail(element)">查看修订详情</el-button>
              <el-button type="text" v-if="element.recoverState === '0'" @click="updateReport(element)">修订报告</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
    </el-table>
    <div style="float: right">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"/>
    </div>

    <!-- 查看修订原因 -->
    <el-dialog title="查看修订原因" :visible.sync="recoverVisible" center width="30%" :close-on-click-modal="false">
      <div>
        <el-form :model="recoverForm" label-width="90px">
          <el-row>
            <el-col :span="23">
              <el-form-item prop="recoverContext" label="修订原因:">
                <el-input type="textarea" v-model="recoverForm.recoverContext" :rows="4" maxlength="300" disabled />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="recoverVisible=false">取消</el-button>
      </div>
    </el-dialog>

    <!-- 查看作废原因 -->
    <el-dialog title="查看作废原因" :visible.sync="repealVisible" center width="30%" :close-on-click-modal="false">
      <div>
        <el-form :model="repealForm" label-width="90px">
          <el-row>
            <el-col :span="23">
              <el-form-item prop="recoverContext" label="作废原因:">
                <el-input type="textarea" v-model="repealForm.recoverContext" :rows="4" maxlength="300" disabled />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="repealVisible=false">取消</el-button>
      </div>
    </el-dialog>

    <!-- 查看修订详情 -->
    <el-dialog title="查看修订详情" :visible.sync="revisionVisible" center width="40%" :close-on-click-modal="false">
      <div>
        <el-form :model="revisionForm" label-width="130px">
          <el-row>
            <el-col :span="11">
              <el-form-item prop="reportMakeUserName" label="报告编制员:">
                <span>{{revisionForm.reportMakeUserName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="reportApproveUserName" label="报告审核员:">
                <span>{{revisionForm.reportApproveUserName}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item prop="reportSignUserName" label="授权签字人:">
                <span>{{revisionForm.reportSignUserName}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item prop="amendContext" label="修订内容:">
                <span>{{revisionForm.amendContext}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item prop="reportFileId" label="修订后报告下载:">
                <el-button type="primary" v-if="revisionForm.reportFileId !== null" @click="clickDownload">点击下载</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="revisionVisible=false">取消</el-button>
      </div>
    </el-dialog>

    <!-- 修订报告 -->
    <el-dialog title="修订报告" :visible.sync="updateVisible" center width="45%" :close-on-click-modal="false">
      <div>
        <el-form ref="updateForm" :model="updateForm" :rules="updateRules" label-width="130px">
          <el-row>
            <el-col :span="11">
              <el-form-item prop="reportMakeUserId" label="报告编制员:">
                <el-select style="width: 100%;" v-model="updateForm.reportMakeUserId" clearable placeholder="请选择报告编制员" @change="getUserIdOne()">
                  <el-option
                    v-for="item in userIdArrayOne"
                    :key="item.userId"
                    :label="item.empName"
                    :value="item.userId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="reportApproveUserId" label="报告审核员:">
                <el-select style="width: 100%;" v-model="updateForm.reportApproveUserId" clearable placeholder="请选择报告审核员" @change="getUserIdTwo()">
                  <el-option
                    v-for="item in userIdArrayTwo"
                    :key="item.userId"
                    :label="item.empName"
                    :value="item.userId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item prop="reportSignUserId" label="授权签字人:">
                <el-select style="width: 100%;" v-model="updateForm.reportSignUserId" clearable placeholder="请选择授权签字人" @change="getUserIdThree()">
                  <el-option
                    v-for="item in userIdArrayThree"
                    :key="item.userId"
                    :label="item.empName"
                    :value="item.userId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item prop="amendContext" label="修订内容:">
                <el-input v-model="updateForm.amendContext" type="textarea" :rows="4" show-word-limit placeholder="修订内容" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item prop="amendContext" label="上传报告:">
                 <el-upload
                   ref="upload"
                   drag
                   class="upload-demo"
                   action="/api/reportRecover/uploadReportRecovery"
                   :on-change="onUploadChange"
                   :on-exceed="onExceed"
                   :on-remove="onUploadRemove"
                   :limit="1"
                   accept=".doc,.docx,.pdf,.xls,.xlsx">
                   <div><i class="el-icon-upload" /></div>
                   <div class="el-upload__text" style="margin-top: 4px;">将文件拖到此处，或<em>点击上传</em></div>
                 </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="updateVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmReport">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import * as recoveryServer from '@/api/reportRecovery'

export default {
  components: { Pagination },

  data() {
    return {
      searchForm: {
        fuzzy: '',
        pageNumber: 1,
        pageSize: 10
      },
      total: 0,
      recoveryData: [],
      recoverVisible: false,
      recoverForm: {
        recoverContext: ''
      },
      repealVisible: false,
      repealForm: {
        recoverContext: ''
      },
      revisionVisible: false,
      revisionForm: {
        reportMakeUserName: '',
        reportApproveUserName: '',
        reportSignUserName: '',
        amendContext: '',
        reportFileId: ''
      },
      updateRules: {
        reportMakeUserId: [{ required: true, message: '请选择报告编制员', trigger: 'blur' }],
        reportApproveUserId: [{ required: true, message: '请选择报告审核员', trigger: 'blur' }],
        reportSignUserId: [{ required: true, message: '请选择授权签字人', trigger: 'blur' }]
      },
      updateVisible: false,
      updateForm: {
        id: '',
        reportMakeUserId: '',
        reportApproveUserId: '',
        reportSignUserId: '',
        amendContext: '',
        reportFileId: ''
      },
      userIdArrayOne: [],
      userIdArrayTwo: [],
      userIdArrayThree: [],

    }
  },

  created() {
    this.getUserIdOne()
    this.getUserIdTwo()
    this.getUserIdThree()
    this.handleSearch()
  },

  methods: {
    handleSearch() {
      recoveryServer.findReportRecover(this.searchForm).then(res => {
        if (res.code === 200 && res.result) {
          this.recoveryData = res.result.records || []
          this.total = res.result.total
        }
      })
    },

    // 查看修订原因
    seeRecover(row) {
      this.recoverForm.recoverContext = row.recoverContext
      this.recoverVisible = true
    },

    // 查看作废原因
    seeRepeal(row) {
      this.repealForm.recoverContext = row.recoverContext
      this.repealVisible = true
    },

    // 查看修订详情
    revisionDetail(row) {
      this.revisionForm.reportMakeUserName = row.reportMakeUserName
      this.revisionForm.reportApproveUserName = row.reportApproveUserName
      this.revisionForm.reportSignUserName = row.reportSignUserName
      this.revisionForm.amendContext = row.amendContext
      this.revisionForm.reportFileId = row.reportFileId
      this.revisionVisible = true
    },

    // 点击下载
    clickDownload() {
      var url = this.$store.getters.sysBaseUrl + '/file/ht/upload/' + this.revisionForm.reportFileId
      window.open(url)
    },

    // 查询报告编制员
    getUserIdOne() {
      recoveryServer.findReportRecoverUser('1').then(res => {
        this.userIdArrayOne = res.result || []
      })
    },

    // 查询报告审核员
    getUserIdTwo() {
      recoveryServer.findReportRecoverUser('2').then(res => {
        this.userIdArrayTwo = res.result || []
      })
    },

    // 查询授权签字人
    getUserIdThree() {
      recoveryServer.findReportRecoverUser('3').then(res => {
        this.userIdArrayThree = res.result || []
      })
    },

    onUploadChange(file) {
      if (file.status === 'ready') return
      if (file.status === 'success') {
        const res = file.response || {}
        if (res && res.code === 200) {
          this.updateForm.reportFileId = file.response.result
          this.$message({
            type: 'success',
            message: '上传成功!'
          })
        } else {
          this.$message.error('上传失败!')
        }
      } else {
        this.$message.error('上传失败!')
      }
    },

    onExceed() {
      this.$message.error('最多上传一个文件')
    },

    onUploadRemove() {
      this.updateForm.reportFileId = ''
    },

    // 修订报告
    updateReport(row) {
      this.updateForm.id = row.id
      this.updateForm.reportMakeUserId = row.reportMakeUserId
      this.updateForm.reportApproveUserId = row.reportApproveUserId
      this.updateForm.reportSignUserId = row.reportSignUserId
      this.updateForm.amendContext = row.amendContext
      this.updateForm.reportFileId = row.reportFileId
      this.updateVisible = true
    },

    // 保存报告
    confirmReport() {
      this.$refs.updateForm.validate(valid => {
        if (valid) {
          recoveryServer.reportAmend(this.updateForm).then(res => {
            if (res.code === 200) {
              this.$notify({
                title: '成功',
                message: res.message,
                type: 'success'
              })
              this.updateVisible = false;
              this.$refs.updateForm.resetFields()
              this.$refs.upload.uploadFiles.splice(0, 1);
              this.handleSearch();
            } else {
              this.$notify({
                title: '失败',
                message: res.message,
                type: 'error'
              })
            }
          })
        }
      })
    },

  }
}
</script>

<style scoped lang="scss" type="text/scss">
.search_div {
  >div {
    &:nth-child(1){
      margin-right: 10px;
    }
    display: inline-block;
  }
}
.search_input {
  width: 200px;
  margin-bottom: 5px;
}
.search-right {
  text-align: right;
  padding-right: 25px;
}
.item {
  font-size: 16px;
}
::v-deep .el-card .el-card__header {
  padding: 5px 20px;
  background: #F3F2F2;
}
.apostrophe{
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.el-button{
  user-select: unset;
}
///deep/ .el-upload-dragger{
  //height: 45px;
  //width: 295px;
//}
</style>
